package com.example.keepalivedemo;

import android.annotation.SuppressLint;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.PowerManager;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private final String TAG = getClass().getSimpleName();

    private final int REQUEST_IGNORE_BATTERY_OPTIMIZATION = 0x00;

    private Button openMIUIBatteryOptBtn;
    private Button openMIUIAutoRunOptBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findView();
        initView();
        initData();
    }

    private void initView() {
        openMIUIBatteryOptBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Build.BRAND != null && Build.BRAND.toLowerCase().equals("xiaomi")) {
                    Intent intent = new Intent();
                    intent.setComponent(new ComponentName("com.miui.powerkeeper", "com.miui.powerkeeper.ui.HiddenAppsContainerManagementActivity"));
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
            }
        });
        openMIUIAutoRunOptBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Build.BRAND != null && Build.BRAND.toLowerCase().equals("xiaomi")) {
                    Intent intent = new Intent();
                    intent.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"));
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
            }
        });
    }

    private void findView() {
        openMIUIBatteryOptBtn = findViewById(R.id.activity_main_open_miui_battery_opt_btn);
        openMIUIAutoRunOptBtn = findViewById(R.id.activity_main_open_miui_autorun_opt_btn);
    }

    private void initData() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!isIgnoreBatteryOptimizationStatus()) {
                requireIgnoreBatteryOptimization();
            }
        }
        startService(new Intent(MainActivity.this, CounterService.class));
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    private boolean isIgnoreBatteryOptimizationStatus() {
        PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
        return powerManager != null && powerManager.isIgnoringBatteryOptimizations(getPackageName());
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    private void requireIgnoreBatteryOptimization() {
        try {
            @SuppressLint("BatteryLife") Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
            intent.setData(Uri.parse("package:" + getPackageName()));
            startActivityForResult(intent, REQUEST_IGNORE_BATTERY_OPTIMIZATION);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_IGNORE_BATTERY_OPTIMIZATION) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                Log.d(TAG, "Ignore Battery Optimization Status is " + isIgnoreBatteryOptimizationStatus());
            }
        }
    }
}
